package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.JobTepiDetailsActivity;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.JobProjectBean;
import com.wnhz.work.yz.view.CircularImage;

import org.xutils.x;

import java.util.List;

/**
 * 作者： zxb
 * 时间： 2017/2/21
 * 描述：
 */
public class JobItemAdapter extends BaseAdapter implements View.OnClickListener {
    //    implements BaseRVAdapter.OnItemClickListener
//{
//        List<JobProjectBean.InfoBean> jobBeanList;
//
//
//
//    /**
//     * 构造器
//     *
//     * @param context 上下文
//     * @param list    数据集合
//     */
//    public JobItemAdapter(Context context, List<JobProjectBean.InfoBean> list) {
//        super(context, list);
//         Toast.makeText(context, ""+list.size(), Toast.LENGTH_SHORT).show();
//
//        this.jobBeanList = list;
//    }
//
//    @Override
//    public int getLayoutId(int viewType) {
//        return R.layout.approval_job;
//    }
//    @Override
//    public void onBind(BaseViewHolder holder, final int position) {
//        x.image().bind(holder.getImageView(R.id.iv_img), jobBeanList.get(position).getPerson_img());
//        holder.setTextView(R.id.tv_name, jobBeanList.get(position).getClassname());
//        holder.setTextView(R.id.tv_apply, jobBeanList.get(position).getApply_id());
//        switch (Integer.parseInt(jobBeanList.get(position).getTepi_status())) {
//            case 0:
//            case 1:
//                holder.setTextView(R.id.tv_authentication, "待审核");
//                break;
//            case 2:
//                holder.setTextView(R.id.tv_authentication, "已通过");
//                break;
//            case 3:
//                holder.setTextView(R.id.tv_authentication, "已驳回");
//                break;
//            case 4:
//                holder.setTextView(R.id.tv_authentication, "已处理");
//                break;
//        }
//        holder.setTextView(R.id.tv_time, jobBeanList.get(position).getCreat_time());
//
//        final List<JobProjectBean.InfoBean.PackageBean> pack = jobBeanList.get(position).getPackageX();
//        RecyclerView recy = holder.getView(R.id.ll_recycler);
//        if (pack.size() == 0) {
//            recy.setVisibility(View.GONE);
//        }else{
//            recy.setVisibility(View.VISIBLE);
//        }
//        holder.setTextView(R.id.size, "共" + pack.size() + "套");
//
//        recy.setAdapter(new BaseRVAdapter(mContext, pack) {
//            @Override
//            public int getLayoutId(int viewType) {
//                return R.layout.item_pack;
//            }
//
//            @Override
//            public void onBind(BaseViewHolder holder, int position) {
//                x.image().bind(holder.getImageView(R.id.img), pack.get(position).getLogo_pic());
//
//            }
//        });
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        recy.setLayoutManager(linearLayoutManager);
//        recy.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(mContext , ""+position, Toast.LENGTH_SHORT).show();
//            }
//        });
//    }
//
//    @Override
//    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//
//    }
//
//    @Override
//    public void itemSelect(int position) {
//        Toast.makeText(mContext, "111---" + position, Toast.LENGTH_SHORT).show();
//    }
    private Context context;
    private List<JobProjectBean.InfoBean> jobBeanList;

    public JobItemAdapter(Context context, List<JobProjectBean.InfoBean> jobBeanList) {
        this.context = context;
        this.jobBeanList = jobBeanList;
    }

    @Override
    public int getCount() {
        return jobBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return jobBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    LinearLayoutManager linearLayoutManager;
    private int index;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        JobItemHolder holder;
        if (convertView == null) {
            holder = new JobItemHolder();
            convertView = View.inflate(context, R.layout.approval_job, null);
//            holder.iv_head = (ImageView) convertView.findViewById(R.id.iv_head);
            holder.iv_img1 = (ImageView) convertView.findViewById(R.id.iv_img1);
            holder.tv_member = (TextView) convertView.findViewById(R.id.tv_member);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_apply = (TextView) convertView.findViewById(R.id.tv_apply);
            holder.tv_authentication = (TextView) convertView.findViewById(R.id.tv_authentication);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.recycler = (RecyclerView) convertView.findViewById(R.id.recycler);
            holder.tepi_detail = (RelativeLayout) convertView.findViewById(R.id.tepi_detail);
            holder.ll_recycler = (LinearLayout) convertView.findViewById(R.id.ll_recycler);
            holder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
            holder.size = (TextView) convertView.findViewById(R.id.size);
            holder.cover_user_photo = (CircularImage) convertView.findViewById(R.id.iv_head);
            holder.hetong_num = (TextView) convertView.findViewById(R.id.hetong_num);
            holder.project_name = (TextView) convertView.findViewById(R.id.project_name);
            convertView.setTag(R.id.job_item_holder, holder);
        } else {
            holder = (JobItemHolder) convertView.getTag(R.id.job_item_holder);
        }
        holder.ll.setOnClickListener(this);
        holder.ll.setTag(jobBeanList.get(position));
//        holder.iv_img1.setOnClickListener(this);
//        holder.tv_name.setOnClickListener(this);
//        holder.tv_apply.setOnClickListener(this);
//        holder.tv_authentication.setOnClickListener(this);
//        holder.tv_time.setOnClickListener(this);
//        holder.recycler.setOnClickListener(this);
//        holder.tepi_detail.setOnClickListener(this);
//        holder.ll_recycler.setOnClickListener(this);
//        holder.size.setOnClickListener(this);


        index = position;
        String str = "合同号：<font color=\"#008c99\">" + jobBeanList.get(position).getContract_num() + "</font>";
        holder.hetong_num.setText(Html.fromHtml(str));
        String str2 = "项目名称：<font color=\"#e9573d\">" + jobBeanList.get(position).getRecord_name() + "</font>";
        holder.project_name.setText(Html.fromHtml(str2));
//        ImageView c=holder.iv_head;
        x.image().bind(holder.cover_user_photo, jobBeanList.get(position).getPerson_img());
        holder.tv_name.setText(jobBeanList.get(position).getRealname());
        holder.tv_member.setText(jobBeanList.get(position).getClassname());
        holder.tv_apply.setText("已申请过" + jobBeanList.get(position).getAll_tepi() + "次特批");
        if ("1".equals(jobBeanList.get(position).getTepi_status())) {
            holder.tv_authentication.setText("待审核");
        } else if ("2".equals(jobBeanList.get(position).getTepi_status())) {
            holder.tv_authentication.setText("已通过");
        } else if ("3".equals(jobBeanList.get(position).getTepi_status())) {
            holder.tv_authentication.setText("已驳回");
        } else if ("4".equals(jobBeanList.get(position).getTepi_status())) {
            holder.tv_authentication.setText("已处理");
        } else {
            holder.tv_authentication.setText("待审核");
        }
        holder.tv_time.setText(jobBeanList.get(position).getCreat_time());
        final List<JobProjectBean.InfoBean.PackageBean> pack = jobBeanList.get(position).getPackageX();
        if (pack.size() == 0) {
            holder.ll_recycler.setVisibility(View.GONE);
        }
        holder.size.setText("共" + pack.size() + "套");
        holder.size.setVisibility(View.GONE);
        holder.recycler.setAdapter(new BaseRVAdapter(context, pack) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_pack;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.img), pack.get(position).getLogo_pic());
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.recycler.setLayoutManager(linearLayoutManager);
        holder.recycler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JobProjectBean.InfoBean info2 = jobBeanList.get(position);
                Log.e("----", "-------------");
                Intent intent = new Intent(context, JobTepiDetailsActivity.class);
                intent.putExtra("bean", info2);
                intent.putExtra("total_tepi_stutas", info2.getTepi_status());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll) {
            JobProjectBean.InfoBean info2 = (JobProjectBean.InfoBean) v.getTag();
            Intent intent = new Intent(context, JobTepiDetailsActivity.class);
            intent.putExtra("bean", info2);
            intent.putExtra("total_tepi_stutas", info2.getTepi_status());
            context.startActivity(intent);
        }
    }

    class JobItemHolder {
        ImageView iv_head, iv_img1;
        TextView tv_name, tv_apply, tv_time, tv_authentication, tv_member;
        RecyclerView recycler;
        RelativeLayout tepi_detail;
        LinearLayout ll_recycler, ll;
        TextView size;
        CircularImage cover_user_photo;
        TextView hetong_num, project_name;

    }
}
